package com.game.activity;

import com.example.quest.R;
import com.game.activity.pager.MainPagerAdapter;
import com.manager.QuestMainService;
import com.manager.QuestUserInterfaceService;
import com.manager.UIHandler;
import com.manager.ViewManager;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.view.ViewPager;

import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private static final String TAG = "MainActivity";
	private Intent UIService;
	
	public static final Object managerSynchLock = new Object();
	public static final Object UISynchLock = new Object();
	
	public static UIHandler UIHandler;
	public static ViewManager vm;
	public static Thread vmThread;
	
	public MainPagerAdapter mainAdapter;
    public ViewPager mainPager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.activity_loading);

		/* Start service */
		Intent mainService = new Intent(this, QuestMainService.class);
		this.startService(mainService);
		
		UIHandler = new UIHandler(this);
		
		vm = new ViewManager();
		vmThread = new Thread(vm);
		vmThread.start();
		
		UIService = new Intent(this, QuestUserInterfaceService.class);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	/* This method receives broadcasts. A broadcast has
	 * an Intent object with it which is passed to the
	 * updateUI method.
	 */
	private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        	updateUI(intent);       
        }
    };
    
    /* Updates the UI by getting objects from the Intent and
     * setting parts of the UI to display them.
     */
    private void updateUI(Intent intent) {
    	String characterName = intent.getStringExtra("characterName"); 
    	try {
	    	TextView txtCharacterName = (TextView) findViewById(R.id.characterTextView);  	
	    	txtCharacterName.setText(characterName);
    	} catch (Exception e) {
    		//Log.d(TAG, "Couldn't find view in layout!");
    		//e.printStackTrace();
    	}
    }
    
    /* Starts the UIService and registers a new receiver to the 
     * broadcastReceiver method. Along with a filter which is
     * the BROADCAST_ACTION constant. */
    @Override
	public void onResume() {
		super.onResume();		
		startService(UIService);
		registerReceiver(broadcastReceiver, new IntentFilter(QuestUserInterfaceService.BROADCAST_ACTION));
	}
 
    /* Unregisters the receiver and stops the UIService. */
	@Override
	public void onPause() {
		super.onPause();
		unregisterReceiver(broadcastReceiver);
		stopService(UIService);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}